<?php

namespace app\api\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class LivestreamerList extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'livestreamer_list';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;


//    // 自动写入时间戳字段
//    protected $autoWriteTimestamp = 'int';
//
//    // 定义时间戳字段名
//    protected $createTime = 'createtime';
//    protected $updateTime = 'updatetime';
//    protected $deleteTime = 'deletetime';

    //查询存储过程数据
    //本方法是针对存储过程中有游标的
    public function procedure_data_select2($params=[],$must_param=[],$procedure_name='',$out_column=[],$in_out_column=[])
    {
        $list = [];
        if($params && $must_param && $procedure_name) {

            $set = preDealProcedureParam($params, $must_param, $out_column);
            $sql = [];
            if ($in_out_column) {
                $sql[] = dealProcedureInOutColumn($in_out_column, $params);

            }
            $sql[] = " call {$procedure_name}($set)";
            $list = Db::query2($sql);

        }
        return $list;
    }

    //查询存储过程数据
    public function procedure_data_select($params=[],$must_param=[],$procedure_name='',$out_column=[],$in_out_column=[])
    {
        $list = [];
        if($params && $must_param && $procedure_name){

            $set = preDealProcedureParam($params,$must_param,$out_column,$in_out_column);
            my_debug(1,'$set--43');
            my_debug(1,$set);
            if($in_out_column){
                $sql =  dealProcedureInOutColumn($in_out_column,$params);
                Db::query($sql);
            }

            $sql = " call {$procedure_name}($set)";
            my_debug(1,'$sql--51');
            my_debug(1,$sql);
            $list = Db::query($sql);

            my_debug(1,'$list--56');
            my_debug(1,$list);

            if(is_array($list) && count($list)){
                return $list;
            }
            if($out_column){
                $rs_out = dealProcedureOutColumn($out_column);
                $sql = "select {$rs_out}";
            }else{
                $sql = "select @_res";
            }
            my_debug(1,'$sql--68');
            my_debug(1,$sql);

            $list = Db::query($sql);
            my_debug(1,'$list--72');
            my_debug(1,$list);
            $list = dealWithProcedureColumn($list,$must_param);
            my_debug(1,'$list--75');
            my_debug(1,$list);
        }
        if(count($list)==1 && isset($list[0])) $list = $list[0];
        return $list;
    }

    //添加存储过程数据
    public function procedure_data_add($params=[],$must_param=[],$procedure_name='',$out_column=[])
    {
        $rs = [];
        if($params && $must_param && $procedure_name){
            $set = preDealProcedureParam($params,$must_param,$out_column);
            $sql = "call {$procedure_name}($set)";

            my_debug(1,'$sql--86');
            my_debug(1,$sql);
            $rs = Db::query($sql);

            my_debug(1,'$rs--90');
            my_debug(1,$rs);

            $sql = "select {$set}";
            my_debug(1,'$sql--94');
            my_debug(1,$sql);
            $rs = Db::query($sql);

            my_debug(1,'$rs--98');
            my_debug(1,$rs);
            $rs = dealWithProcedureColumn($rs,$must_param);
            if(count($rs) == 1){
                $rs = $rs[0];
            }
        }
        return $rs;
    }


}
